package com.fitso.wicket.page.journal;

import static com.fitso.wicket.util.FitsoWicketUtils.toLoadableDetachableModel;

import java.util.Iterator;

import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.fitso.model.bean.User;
import com.fitso.model.bean.journal.FoodJournal;
import com.fitso.model.bean.journal.FoodJournalEntry;
import com.fitso.model.service.food.journal.FoodJournalService;

public class FoodJournalDataProvider implements IDataProvider<FoodJournalEntry> {

	private FoodJournal _foodJournal;

	@SpringBean(name = "FoodJournalService")
	private FoodJournalService _foodJournalService;

	public FoodJournalDataProvider(User user, DateTime dateTime) {
		_foodJournal = _foodJournalService.fetch(user, dateTime);

		if (_foodJournal == null) {
			_foodJournal = new FoodJournal();
		}
	}

	public void detach() {
		// do any clean up
	}

	public Iterator<FoodJournalEntry> iterator(int first, int count) {
		return _foodJournal.getEntries().iterator();
	}
	
	public int size() {
		return _foodJournal.getEntries().size();
	}

	public IModel<FoodJournalEntry> model(FoodJournalEntry entry) {
		// wrap a given entry in a model
		return toLoadableDetachableModel(entry, FoodJournalEntry.class);
	}
}